using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class PhotosDAO
	{
		public const string PHOTOS_GET_BY_ID = "PhotosGetByID";
		public const string PHOTOS_GET_ALL = "PhotosGetAll";
		public const string PHOTOS_INSERT = "PhotosInsert";
		public const string PHOTOS_UPDATE_BY_ID = "PhotosUpdateByID";
		public const string PHOTOS_DELETE_BY_ID = "PhotosDeleteByID";

		public const string PHOTOSID = "PhotosID";
		public const string THUMBNAIL = "thumbnail";
		public const string PREVIEW = "preview";
		public const string PRODUCTSID = "ProductsID";
		public const string CREATEDATE = "Createdate";

        public const string PHOTOS_GET_BY_PRODUCTSID = "PhotosGetByProductsId"; 

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public PhotosDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public PhotosDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(PHOTOS_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, PHOTOSID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetPhotosFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<PhotosDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(PHOTOS_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<PhotosDTO>();
				while (dataReader.Read())
				{
					var item = GetPhotosFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(PhotosDTO photos)
		{
			var command = Database.GetStoredProcCommand(PHOTOS_INSERT);
			try
			{
				Database.AddOutParameter(command, PHOTOSID, DbType.String, 36);
				Database.AddInParameter(command, THUMBNAIL, DbType.String, photos.thumbnail);
				Database.AddInParameter(command, PREVIEW, DbType.String, photos.preview);
				Database.AddInParameter(command, PRODUCTSID, DbType.String, photos.ProductsID);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, photos.Createdate);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, PHOTOSID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(PhotosDTO photos)
		{
			var command = Database.GetStoredProcCommand(PHOTOS_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, PHOTOSID, DbType.String, photos.PhotosID);
				Database.AddInParameter(command, THUMBNAIL, DbType.String, photos.thumbnail);
				Database.AddInParameter(command, PREVIEW, DbType.String, photos.preview);
				Database.AddInParameter(command, PRODUCTSID, DbType.String, photos.ProductsID);
				Database.AddInParameter(command, CREATEDATE, DbType.String, photos.Createdate);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string photosid)
		{
			var command = Database.GetStoredProcCommand(PHOTOS_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, PHOTOSID, DbType.String, photosid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static PhotosDTO GetPhotosFromDataReader(IDataRecord dataReader)
		{
			var item = new PhotosDTO();
			try
			{

				item.PhotosID = (string.IsNullOrEmpty(Convert.ToString(dataReader[PHOTOSID]))) ? "" : Convert.ToString(dataReader[PHOTOSID]);
				item.thumbnail = (string.IsNullOrEmpty(Convert.ToString(dataReader[THUMBNAIL]))) ? "" : Convert.ToString(dataReader[THUMBNAIL]);
				item.preview = (string.IsNullOrEmpty(Convert.ToString(dataReader[PREVIEW]))) ? "" : Convert.ToString(dataReader[PREVIEW]);
				item.ProductsID = (string.IsNullOrEmpty(Convert.ToString(dataReader[PRODUCTSID]))) ? "" : Convert.ToString(dataReader[PRODUCTSID]);
				item.Createdate = (string.IsNullOrEmpty(Convert.ToString(dataReader[CREATEDATE]))) ? DateTime.MinValue : DateTime.Parse(dataReader[CREATEDATE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
        public IList<PhotosDTO> GetByProductId(string productId)
        {
            var command = Database.GetStoredProcCommand(PHOTOS_GET_BY_PRODUCTSID);
            Database.AddInParameter(command, PRODUCTSID, DbType.String, productId);
            try
            {
                var dataReader = Database.ExecuteReader(command);
                var lstItem = new List<PhotosDTO>();
                while (dataReader.Read())
                {
                    var item = GetPhotosFromDataReader(dataReader);
                    lstItem.Add(item);
                }
                return lstItem;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
	}
}

